package com.infindo.iflower;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.infindo.common.Constant;
import com.infindo.iflower.util.AssetsUtil;

public class ImageAdapter extends BaseAdapter{
	private Context mContext;
//	private Integer[] mThumbIds = new Integer[20];
	private String[] picNames;
	private Activity myActivity;
	private String type;
	public ImageAdapter(Context c,Activity activity, String[] picNames,String filetype) {
		mContext = c;
		this.picNames = picNames;
		this.myActivity = activity;
		this.type = filetype;
	}

	public int getCount() {
		return picNames.length;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		ImageView imageView;
		if (convertView == null) {
			imageView = new ImageView(mContext);
			imageView.setLayoutParams(new GridView.LayoutParams(
					GridView.LayoutParams.FILL_PARENT,
					GridView.LayoutParams.FILL_PARENT));
			imageView.setAdjustViewBounds(true);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setPadding(0, 0, 0, 0);

		} else {
			imageView = (ImageView) convertView;
		}
		imageView.setLayoutParams(new GridView.LayoutParams(75, 75));
		if(Constant.FILENAME_FROM_FAVORITE.equals(type)){
			FileInputStream in = null;
			try {
				in = new FileInputStream(picNames[position]);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			imageView.setImageDrawable(new BitmapDrawable(in));
		}
		else{
			imageView.setImageDrawable(AssetsUtil.getPictureDrawable(myActivity,picNames[position]));
		}
		imageView.setTag(position);

		return imageView;
	}

}
